package com.example.mall.web.controller;

import com.example.mall.domain.InventoryAlerts;
import com.example.mall.dto.InventoryAlertsDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.InventoryAlertsQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IInventoryAlertsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/inventoryAlerts")
public class InventoryAlertsController {

    @Autowired
    public IInventoryAlertsService inventoryAlertsService;

    /**
     * 分页查询库存预警表信息
     * @param inventoryAlertsQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询库存预警表信息",notes = "分页查询库存预警表信息",httpMethod = "GET")
    public JSONResult queryPage(InventoryAlertsQuery inventoryAlertsQuery){
        PageDTO<InventoryAlertsDto> inventoryAlertsDtoPageDTO = inventoryAlertsService.queryPage(inventoryAlertsQuery);
        return JSONResult.success("查询成功",inventoryAlertsDtoPageDTO);
    }

    /**
     * 根据id查询库存预警表信息
     * @param alertId
     * @return
     */
    @GetMapping("query/{alertId}")
    @ApiOperation(value = "根据id查询库存预警表信息",notes = "根据id查询库存预警表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("alertId")Long alertId){
        //1.根据id查询用户信息
        if (alertId == null){
            throw new GlobalCustomerException(ErrorEnumCode.ALERTID_NOT_ID);
        }
        InventoryAlerts inventoryAlerts = inventoryAlertsService.getById(alertId);
        return JSONResult.success("查询成功",inventoryAlerts);
    }

    /**
     * 根据id查询库存预警表信息
     * @param alertId
     * @return
     */
    @GetMapping("queryInventoryAlertsById/{alertId}")
    @ApiOperation(value = "根据id查询库存预警表信息",notes = "根据id查询库存预警表信息",httpMethod = "GET")
    public JSONResult queryInventoryAlertsById(@PathVariable("alertId")Long alertId){
        //1.根据id查询用户信息
        if (alertId == null){
            throw new GlobalCustomerException(ErrorEnumCode.ALERTID_NOT_ID);
        }
        InventoryAlerts inventoryAlerts = inventoryAlertsService.queryInventoryAlertsById(alertId);
        return JSONResult.success("查询成功",inventoryAlerts);
    }

    /**
     * 查询全部库存预警表信息
     * @param
     * @return
     */
    @GetMapping("queryInventoryAlertsList")
    @ApiOperation(value = "查询全部库存预警表信息",notes = "查询全部库存预警表信息",httpMethod = "GET")
    public JSONResult queryInventoryAlertsList(){
        List<InventoryAlerts> inventoryAlerts = inventoryAlertsService.queryInventoryAlertsList();
        return JSONResult.success("查询成功",inventoryAlerts);
    }

    /**
     * 新增库存预警表
     * @param inventoryAlertsDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增库存预警表",notes = "新增库存预警表",httpMethod = "POST")
    public JSONResult insert(@RequestBody InventoryAlertsDto inventoryAlertsDto){
        inventoryAlertsService.insert(inventoryAlertsDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改库存预警表信息
     * @param inventoryAlertsDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改库存预警表信息",notes = "修改库存预警表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody InventoryAlertsDto inventoryAlertsDto){
        inventoryAlertsService.update(inventoryAlertsDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除库存预警表信息
     * @param alertId
     * @return
     */
    @PostMapping("delete/{alertId}")
    @ApiOperation(value = "删除库存预警表信息",notes = "删除库存预警表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("alertId") Long alertId) {
        inventoryAlertsService.delete(alertId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除库存预警表信息
     * @param alertIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除库存预警表信息",notes = "批量删除库存预警表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> alertIds) {
        inventoryAlertsService.deleteBatch(alertIds);
        return JSONResult.success("批量删除成功");
    }
}
